import Http from 'axios'
import API from '@/config/API-config'
import Qs from 'qs'
import { Indicator } from 'mint-ui';
// Http.defaults.headers.post['Content-Type'] = "application/json;charset=utf-8"; // json格式传输数据
// Http.defaults.withCredentials=true;
// delete $http.Provider.default.headers.common['X-Requested-With'];
Http.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded;charset=UTF-8"; //formdata格式传输数据
Http.defaults.headers.common['X-Requested-With']="XMLHttpRequest";


const encodeParams = (params) => {
    let r = '?',
        p = [];
    for(let key in params) {
        p.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`);
    }
    return r + p.join('&&');
};

//请求类
class ApiService {
  constructor() {
    //设置session
    this.session = {
      setSession: this.post.bind(this, API.SETSSION.setSession),
      getSession: this.get.bind(this, API.SETSSION.getSession),
    };
    //登录/获取侧边栏接口
    this.login = {
      login: this.post.bind(this, API.LOGIN.login),
      listMenu: this.post.bind(this, API.LOGIN.listMenu),
    };
    this.transport = {
      load: this.get.bind(this, API.TRANSPORT.load),//承运商运单列表
      detail: this.get.bind(this, API.TRANSPORT.detail),//承运商运单详情
      arrived:this.post.bind(this, API.TRANSPORT.arrived),//承运商已送达
      waybills:this.get.bind(this, API.TRANSPORT.waybills),//运单数量
      getCountNum:this.get.bind(this, API.TRANSPORT.getCountNum),//列表数量
    };
    this.auction = {
      load: this.get.bind(this, API.AUCTION.load),//竞价列表
    };
    this.quotation = {
      offer: this.post.bind(this, API.QUOTATUIN.offer),//确认报价
      getQuotationInfo:this.get.bind(this, API.QUOTATUIN.getQuotationInfo),//获取报价信息
    };
    this.dispatch = {
      dispatchInfo: this.post.bind(this, API.DISPATCH.dispatchInfo),//调度
      dispatchAdjustInfo: this.post.bind(this, API.DISPATCH.dispatchAdjustInfo),//调度
      add:this.post.bind(this, API.DISPATCH.add),//调度保存
      adjust:this.post.bind(this, API.DISPATCH.adjust),//调度保存
    };
    this.travelUser = {
      list: this.get.bind(this, API.TRAVELUSER.list),//车船列表
      del: this.get.bind(this, API.TRAVELUSER.del),//车船删除
      add: this.post.bind(this, API.TRAVELUSER.add),//车船新增
      modify: this.post.bind(this, API.TRAVELUSER.modify),//车船修改
    };
    this.driver = {
      load: this.get.bind(this, API.DRIVER.load),//司机运单列表
      detail: this.get.bind(this, API.DRIVER.detail),//司机运单详情
      delivered:this.post.bind(this, API.DRIVER.delivered),//司机已送达
      waybills: this.get.bind(this, API.DRIVER.waybills),//司机运单数量
    };
    this.company ={
      detail:this.get.bind(this, API.COMPANY.detail),
      edit:this.post.bind(this, API.COMPANY.edit),
      add:this.post.bind(this, API.COMPANY.add),
      isExist:this.get.bind(this, API.COMPANY.isExist),
    }
    this.receipt = {
      appUpload : this.post.bind(this,API.RECEIPT.appUpload),//app回单上传
      detail:this.get.bind(this,API.RECEIPT.detail),//回单详情
      tspDispatchDetail : this.get.bind(this,API.RECEIPT.tspDispatchDetail),//获取回单调度详情
      dispatchCarrier:this.get.bind(this,API.RECEIPT.dispatchCarrier),//获取司机回单调度详情
      confirm:this.post.bind(this,API.RECEIPT.confirm),//回单审核
      deleteImage:this.post.bind(this,API.RECEIPT.deleteImage),//删除图片
    }
    this.line={
      add: this.post.bind(this,API.LINE.add),
      edit: this.post.bind(this,API.LINE.edit),
      remove:this.post.bind(this,API.LINE.remove),
      load:this.get.bind(this,API.LINE.load)
    }
    this.settle={
      load:this.post.bind(this,API.SETTLE.load),
      detail:this.post.bind(this,API.SETTLE.detail),
      viewInvoice:this.post.bind(this,API.SETTLE.viewInvoice),
      uploadInvoce:this.post.bind(this,API.SETTLE.uploadInvoce),
      invoicing:this.post.bind(this,API.SETTLE.invoicing),
      waybills:this.get.bind(this,API.SETTLE.waybills),
    },this.trace = {
      traceInfo:this.get.bind(this,API.TRACE.getLatitudeLongitude)
    },this.message = {
      load:this.get.bind(this,API.MESSAGE.load),
      setPush:this.post.bind(this,API.MESSAGE.setPush),
      setList:this.get.bind(this,API.MESSAGE.setList),
    }
    // this.interceptorsOfReq();
     this.interceptorsOfRes();
  }

  get(url, params) {
    Indicator.open({
      text: '加载中...',
      spinnerType: 'snake'
    });
    if(params) {
      url += encodeParams(params);
      // url += `&JSID=${localStorage.sessionid}&userID=${localStorage.user? JSON.parse(localStorage.user).userID :''}`;
    }
    // return Http.get(url).then(res => res.data);
    return Http.get(url).then(res => {
      setTimeout(function(){
        Indicator.close();
      },500);
      return res.data
    });
  }

  post(url, data) {
    Indicator.open({
      text: '加载中...',
      spinnerType: 'snake'
    });
    typeof data === 'undefined' ? data = {} : '';
    // data = JSON.stringify(data); //json提交
    data = Qs.stringify(data); // form data 提交
    return Http.post(url, data).then(res => {
      setTimeout(function(){
        Indicator.close();
      },1000);
      return res.data
    });
    // localStorage.sessionid ? data.JSID = localStorage.sessionid : '';
    // data.userID = localStorage.user ? JSON.parse(localStorage.user).userID : '';
  }

  interceptorsOfReq() {
    return Http.interceptors.request.use(
      config => {
        return config;
      },
      err => {
        return Promise.reject(err);
      });
  }

  interceptorsOfRes() {
    Http.interceptors.response.use(function(response) {
     if(response.data== '20'){
          window.location.href='http://www.chuanqi56.com/cq-web/dist/index.html#/audit';
      }
      if(response.data == '30'){
         window.location.href='http://www.chuanqi56.com/cq-web/dist/index.html#/reject';
      }
      if(response.data == '50'){
         window.location.href='http://www.chuanqi56.com/cq-web/dist/index.html#/hangup';
      }
      /*if(response.data.errorCode === 4) {
        localStorage.removeItem('sessionid');
        localStorage.removeItem('user');
        alert('登录失效，请重新登录！！！');
        vueVm.loginPopFlag=true;
      }*/
      return response;
    }, function(error) {
      return Promise.reject(error);
    });
  }

}

//导出一个对象
export default new ApiService();
